package com.uduew.sjd.ui.repayment

import android.os.Bundle
import android.view.View
import com.uduew.sjd.BR
import com.uduew.sjd.R
import com.uduew.sjd.api.PostponeApi
import com.uduew.sjd.bean.HttpBean
import com.uduew.sjd.message.PageMessenger
import com.uduew.sjd.net.DataResult
import com.uduew.sjd.request.PostPoneRequester
import com.uduew.sjd.ui.base.BaseFragment
import com.uduew.sjd.ui.base.StateHolder
import com.uduew.sjd.utils.ToastUtils
import com.uduew.sjd.utils.Utils
import com.kongzue.dialogx.dialogs.WaitDialog
import com.kunminx.architecture.ui.page.DataBindingConfig
import com.kunminx.architecture.ui.state.State
import com.uduew.sjd.api.AllLoanOrdersApi
import com.uduew.sjd.config.Const
import com.uduew.sjd.utils.SpannableUtils
import kotlin.text.toLong

class ExtensionDetailFragment : BaseFragment() {

    lateinit var mState: ExtensionDetailStates
    private lateinit var mMessenger: PageMessenger
    private lateinit var mPostPoneRequester: PostPoneRequester

    override fun initViewModel() {
        mState = getFragmentScopeViewModel(ExtensionDetailStates::class.java)
        mMessenger = getApplicationScopeViewModel(PageMessenger::class.java)
        mPostPoneRequester = getFragmentScopeViewModel(PostPoneRequester::class.java)//展期试算
    }

    var orderDetail: AllLoanOrdersApi.ResAllLoanOrdersBean.MamaguyBean.ThoroughBean? = null
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        orderDetail = arguments?.getParcelable("orderDetail")
        mPostPoneRequester.result.observe(viewLifecycleOwner) { dataResult: DataResult<HttpBean<PostponeApi.PostponeBean>> ->
            WaitDialog.dismiss()
            if (!dataResult.responseStatus.isSuccess) {
                ToastUtils.showShortToast(dataResult.responseStatus.responseCode)
                return@observe
            }
            dataResult.result.data.let {
                mState.nacelle.set(it?.nacelle.toString() + Utils.getScooteristStr(it?.scooterist))
                it?.calculator.let { it1 ->
                    if (it1 != null) {
                        mState.calculator.set(
                            Const.CURRENCY_SYMBOL + SpannableUtils.getDoubleFormat(
                                it1
                            )
                        )
                    }
                }
                it?.shorefront.let { it1 ->
                    if (it1 != null) {
                        mState.shorefront.set(
                            Const.CURRENCY_SYMBOL + SpannableUtils.getDoubleFormat(
                                it1
                            )
                        )
                    }
                }
                it?.gilsonite.let {
                    if (it != null) {
                        val date = Utils.timestampToDate(it.toLong())
                        mState.gilsonite.set(date)
                    }
                }
            }
        }
        orderDetail?.beastly.let {
            if (!it.isNullOrEmpty()) {
                mPostPoneRequester.request(this, it)
            }
        }
    }

    override fun getDataBindingConfig(): DataBindingConfig? {
        return DataBindingConfig(R.layout.fragment_extension_detail, BR.vm, mState).addBindingParam(
            BR.click,
            Click()
        )
    }


    inner class Click {
        fun navBack() {
            if (!Utils.checkDebounce()) return
            nav().navigateUp()
        }

        fun nowRepayment() {
            if (!Utils.checkDebounce()) return
            val bundle = Bundle().apply {
                putString("orderId", orderDetail?.beastly)
                putString("calculator", mState.calculator.get())
            }
            nav().navigate(
                R.id.action_navigation_extension_detail_to_repayment_type_fragment, bundle
            )
        }

        fun rollOverRepayment() {

        }
    }

    class ExtensionDetailStates : StateHolder() {
        val cindy: State<String> = State("") //订单编号
        val atonalism: State<String> = State("") //贷款App Id
        val herzegovina: State<String> = State("")//应还利息
        val awag: State<String> = State("")//逾期利息

        val thalassochemical: State<String> = State("") //到期时间 1757610000000
        val sertoman: State<String> = State("")//展期开始时间 1757610000000
        val gilsonite: State<String> = State("")//展期到期时间 1766250000000
        val gastrology: State<String> = State("")//当前延期成功的数目

        val hebron: State<String> = State("")//展期的周期 100
        val nacelle: State<String> = State("")//限制总延期数 99
        val scooterist: State<String> = State("")//周期单位：DAY-天,MONTH-月

        val calculator: State<String> = State("")//展期当前所需还款额 2711
        val strabismal: State<String> = State("")//管理费 2711

        val shorefront: State<String> = State("") //贷款金额 58420
        val policymaking: State<String> = State("")//合同金额 58420
        val tubulure: State<String> = State("")//还款总额 58420
    }

}